package com.jeedcp.gen.controller;


import com.jeedcp.gen.entity.CreateTableEntity;
import com.jeedcp.gen.model.GenTable;
import com.jeedcp.gen.utils.DBUtil;
import com.jeedcp.gen.utils.R;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/devtools/v1/maintain")
public class MaintainController {

    /**
     * 保存
     */
    @RequestMapping("/table")
    // @RequiresPermissions("alarm:save")
    public R save(@RequestBody CreateTableEntity createTableEntity) throws SQLException, ClassNotFoundException {
        if (createTableEntity.getSql()!=""){
            String sql =createTableEntity.getSql();
            DBUtil.executeUpdateFH(sql);
        }else {
            String sql ="";
            if (createTableEntity.getIdType().equals("DBAI")){
                sql = "CREATE TABLE "+createTableEntity.getNewTableName()+" ( id INT ( 11 ) UNSIGNED auto_increment NOT NULL PRIMARY KEY COMMENT '编号'";
            }else if (createTableEntity.getIdType().equals("SYSGI")){
                sql = "CREATE TABLE "+createTableEntity.getNewTableName()+"(  id BIGINT(19) unsigned  not null primary key comment '编号'";
            }else if (createTableEntity.getIdType().equals("UUID")){
                sql = "CREATE TABLE "+createTableEntity.getNewTableName()+"(  id VARCHAR(32) not null primary key comment '编号'";
            }
            if (createTableEntity.getExtdata().equals("1")){
                sql=sql+", extdata varchar(100) comment '扩展JSON'";
            }
            if (createTableEntity.getActive().equals("1")){
                sql=sql+", active tinyint(1) not null default '1' comment '是否有效'";
            }
            if (createTableEntity.getCreate_by().equals("1")){
                sql=sql+", create_by varchar(100) comment '创建人'";
            }
            if (createTableEntity.getCreate_time().equals("1")){
                sql=sql+", create_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间'";
            }
            if (createTableEntity.getUpdate_by().equals("1")){
                sql=sql+", update_by varchar(100) comment '修改人'";
            }
            if (createTableEntity.getUpdate_time().equals("1")){
                sql=sql+", update_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间'";
            }


            sql=sql+")"+"COMMENT = '"+createTableEntity.getObjName()+"'";
            DBUtil.executeUpdateFH(sql);
        }
        return R.ok("success");
    }
}
